{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean: 3.1500000000000004 diff: 0.22360679774997902\n",
      "mean: 3.144 diff: 0.19090918204600243\n",
      "mean: 3.1600000000000006 diff: 0.17982447582368527\n",
      "mean: 3.182 diff: 0.09151358829215064\n",
      "mean: 3.1912000000000003 diff: 0.06320026648844541\n",
      "mean: 3.1278 diff: 0.06468677567608716\n",
      "mean: 3.1562 diff: 0.017106723824274438\n",
      "                avg       std\n",
      "20 times     3.1500  0.223607\n",
      "50 times     3.1440  0.190909\n",
      "100 times    3.1600  0.179824\n",
      "200 times    3.1820  0.091514\n",
      "500 times    3.1912  0.063200\n",
      "1000 times   3.1278  0.064687\n",
      "5000 times   3.1562  0.017107\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>avg</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>20 times</td>\n",
       "      <td>3.1500</td>\n",
       "      <td>0.223607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50 times</td>\n",
       "      <td>3.1440</td>\n",
       "      <td>0.190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>100 times</td>\n",
       "      <td>3.1600</td>\n",
       "      <td>0.179824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>200 times</td>\n",
       "      <td>3.1820</td>\n",
       "      <td>0.091514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>500 times</td>\n",
       "      <td>3.1912</td>\n",
       "      <td>0.063200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1000 times</td>\n",
       "      <td>3.1278</td>\n",
       "      <td>0.064687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5000 times</td>\n",
       "      <td>3.1562</td>\n",
       "      <td>0.017107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                avg       std\n",
       "20 times     3.1500  0.223607\n",
       "50 times     3.1440  0.190909\n",
       "100 times    3.1600  0.179824\n",
       "200 times    3.1820  0.091514\n",
       "500 times    3.1912  0.063200\n",
       "1000 times   3.1278  0.064687\n",
       "5000 times   3.1562  0.017107"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Monte Carlo Simulation exec1\n",
    "import random\n",
    "import math\n",
    "import pandas as pd\n",
    "\n",
    "Radius = 100\n",
    "\n",
    "def distanceFromOriginDot(x, y):\n",
    "    return math.sqrt(x**2+y**2)\n",
    "\n",
    "def MonteCarlo(dotNum):\n",
    "    redDotNum = 0\n",
    "    greenDotNum = 0\n",
    "    \n",
    "    for num in range(dotNum):\n",
    "        x = random.random()*Radius\n",
    "        y = random.random() * Radius\n",
    "        distance = distanceFromOriginDot(x,y)\n",
    "        if distance <= Radius:\n",
    "            redDotNum += 1\n",
    "        else:\n",
    "            greenDotNum += 1\n",
    "    return redDotNum/dotNum * 4\n",
    "\n",
    "\n",
    "def Iteration(dotNum):\n",
    "    arr = []\n",
    "    for i in range(20):\n",
    "        arr.append(MonteCarlo(dotNum))\n",
    "        \n",
    "    \n",
    "    se = pd.Series(arr)\n",
    "    avg = se.mean()\n",
    "    diff = se.std()\n",
    "    print(\"mean:\", avg, \"diff:\", diff)\n",
    "    return (avg, diff)\n",
    "\n",
    "#exec1\n",
    "def exec1():\n",
    "    avg = []\n",
    "    std = []\n",
    "    \n",
    "    times = [20,50,100,200,500,1000,5000]\n",
    "    \n",
    "    for time in times:\n",
    "        tempavg, tempstd = Iteration(time)\n",
    "        avg.append(tempavg)\n",
    "        std.append(tempstd)\n",
    "    \n",
    " \n",
    "    \n",
    "    se_avg = pd.Series(avg, index=[\"{} times \".format(x) for x in times], name = \"avg\")\n",
    "    se_std = pd.Series(std, index=[\"{} times \".format(x) for x in times], name = \"std\")\n",
    "    \n",
    "    df = pd.concat([se_avg, se_std], axis=1)\n",
    "    print(df)\n",
    "    return df\n",
    "\n",
    "exec1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean: 0.2457575049683995 diff: 0.131428118495145\n",
      "mean: 0.2613115765580971 diff: 0.0884632544840704\n",
      "mean: 0.24983048204052302 diff: 0.06591585207986461\n",
      "mean: 0.25052310145479045 diff: 0.05732649721322318\n",
      "mean: 0.25271199183864324 diff: 0.04517453695603213\n",
      "mean: 0.25113333012910355 diff: 0.042364574949152606\n",
      "mean: 0.2484755242920453 diff: 0.035273702358790654\n",
      "mean: 0.25357784074744744 diff: 0.03142729211882618\n",
      "mean: 0.25316621692950153 diff: 0.03166490079463202\n",
      "mean: 0.2484312419438905 diff: 0.027413022336969933\n",
      "                 avg       std\n",
      "5 times     0.245758  0.131428\n",
      "10 times    0.261312  0.088463\n",
      "20 times    0.249830  0.065916\n",
      "30 times    0.250523  0.057326\n",
      "40 times    0.252712  0.045175\n",
      "50 times    0.251133  0.042365\n",
      "60 times    0.248476  0.035274\n",
      "70 times    0.253578  0.031427\n",
      "80 times    0.253166  0.031665\n",
      "100 times   0.248431  0.027413\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>avg</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>5 times</td>\n",
       "      <td>0.245758</td>\n",
       "      <td>0.131428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10 times</td>\n",
       "      <td>0.261312</td>\n",
       "      <td>0.088463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20 times</td>\n",
       "      <td>0.249830</td>\n",
       "      <td>0.065916</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30 times</td>\n",
       "      <td>0.250523</td>\n",
       "      <td>0.057326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>40 times</td>\n",
       "      <td>0.252712</td>\n",
       "      <td>0.045175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50 times</td>\n",
       "      <td>0.251133</td>\n",
       "      <td>0.042365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60 times</td>\n",
       "      <td>0.248476</td>\n",
       "      <td>0.035274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>70 times</td>\n",
       "      <td>0.253578</td>\n",
       "      <td>0.031427</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>80 times</td>\n",
       "      <td>0.253166</td>\n",
       "      <td>0.031665</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>100 times</td>\n",
       "      <td>0.248431</td>\n",
       "      <td>0.027413</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 avg       std\n",
       "5 times     0.245758  0.131428\n",
       "10 times    0.261312  0.088463\n",
       "20 times    0.249830  0.065916\n",
       "30 times    0.250523  0.057326\n",
       "40 times    0.252712  0.045175\n",
       "50 times    0.251133  0.042365\n",
       "60 times    0.248476  0.035274\n",
       "70 times    0.253578  0.031427\n",
       "80 times    0.253166  0.031665\n",
       "100 times   0.248431  0.027413"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#MonteCarlo Integrate\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "\n",
    "def IntegrateWithMC(sampleNum):\n",
    "    sample = np.random.random(size = sampleNum)\n",
    "    \n",
    "    result =  (1-0)/sampleNum * np.sum(sample**3)\n",
    "    return result\n",
    "def Iteration2(sampleNum):\n",
    "    arr = []\n",
    "    for i in range(100):\n",
    "        arr.append(IntegrateWithMC(sampleNum))\n",
    "    se = pd.Series(arr)\n",
    "    avg = se.mean()\n",
    "    diff = se.std()\n",
    "    print(\"mean:\", avg, \"diff:\", diff)\n",
    "    return (avg, diff)\n",
    "\n",
    "#exec2\n",
    "def exec2():\n",
    "    avg = []\n",
    "    std = []\n",
    "    \n",
    "    times = [5,10,20,30,40,50,60,70,80,100]\n",
    "    \n",
    "    for time in times:\n",
    "        tempavg, tempstd = Iteration2(time)\n",
    "        avg.append(tempavg)\n",
    "        std.append(tempstd)\n",
    "    \n",
    " \n",
    "    \n",
    "    se_avg = pd.Series(avg, index=[\"{} times \".format(x) for x in times], name = \"avg\")\n",
    "    se_std = pd.Series(std, index=[\"{} times \".format(x) for x in times], name = \"std\")\n",
    "    \n",
    "    df = pd.concat([se_avg, se_std], axis=1)\n",
    "    print(df)\n",
    "    return df\n",
    "\n",
    "exec2()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean: 26377.672538889987 diff: 24643.71126978743\n",
      "mean: 28668.033596901267 diff: 21754.52826092279\n",
      "mean: 29409.08897688544 diff: 14923.518279958595\n",
      "mean: 29821.481070735626 diff: 16024.195439782115\n",
      "mean: 29509.093013005233 diff: 12635.688435986287\n",
      "mean: 28298.147706844306 diff: 11389.025899511349\n",
      "mean: 30503.367925805684 diff: 11066.59748731053\n",
      "mean: 28718.22608104888 diff: 10108.087393169184\n",
      "mean: 28484.889677139294 diff: 8848.078731852027\n",
      "mean: 29052.754860185603 diff: 6446.100807027894\n",
      "mean: 29737.558272905713 diff: 4018.0441381121045\n",
      "                     avg           std\n",
      "10 times    26377.672539  24643.711270\n",
      "20 times    28668.033597  21754.528261\n",
      "30 times    29409.088977  14923.518280\n",
      "40 times    29821.481071  16024.195440\n",
      "50 times    29509.093013  12635.688436\n",
      "60 times    28298.147707  11389.025900\n",
      "70 times    30503.367926  11066.597487\n",
      "80 times    28718.226081  10108.087393\n",
      "100 times   28484.889677   8848.078732\n",
      "200 times   29052.754860   6446.100807\n",
      "500 times   29737.558273   4018.044138\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>avg</th>\n",
       "      <th>std</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>10 times</td>\n",
       "      <td>26377.672539</td>\n",
       "      <td>24643.711270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20 times</td>\n",
       "      <td>28668.033597</td>\n",
       "      <td>21754.528261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30 times</td>\n",
       "      <td>29409.088977</td>\n",
       "      <td>14923.518280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>40 times</td>\n",
       "      <td>29821.481071</td>\n",
       "      <td>16024.195440</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50 times</td>\n",
       "      <td>29509.093013</td>\n",
       "      <td>12635.688436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60 times</td>\n",
       "      <td>28298.147707</td>\n",
       "      <td>11389.025900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>70 times</td>\n",
       "      <td>30503.367926</td>\n",
       "      <td>11066.597487</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>80 times</td>\n",
       "      <td>28718.226081</td>\n",
       "      <td>10108.087393</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>100 times</td>\n",
       "      <td>28484.889677</td>\n",
       "      <td>8848.078732</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>200 times</td>\n",
       "      <td>29052.754860</td>\n",
       "      <td>6446.100807</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>500 times</td>\n",
       "      <td>29737.558273</td>\n",
       "      <td>4018.044138</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     avg           std\n",
       "10 times    26377.672539  24643.711270\n",
       "20 times    28668.033597  21754.528261\n",
       "30 times    29409.088977  14923.518280\n",
       "40 times    29821.481071  16024.195440\n",
       "50 times    29509.093013  12635.688436\n",
       "60 times    28298.147707  11389.025900\n",
       "70 times    30503.367926  11066.597487\n",
       "80 times    28718.226081  10108.087393\n",
       "100 times   28484.889677   8848.078732\n",
       "200 times   29052.754860   6446.100807\n",
       "500 times   29737.558273   4018.044138"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#MonteCarlo Integrate2\n",
    "import numpy as np\n",
    "import math\n",
    "\n",
    "\n",
    "def IntegrateWithMC2(sampleNum):\n",
    "    x = 2 * np.random.random(size = sampleNum) + 2\n",
    "    y = 2 * np.random.random(size = sampleNum) - 1\n",
    "    \n",
    "    result = []\n",
    "    for i in range(len(x)):\n",
    "        temp = y[i]**2 * math.exp(-y[i]**2)/(x[i]**2 * np.exp(-x[i]**2)) + x[i]**3\n",
    "        result.append(temp)  \n",
    "    \n",
    "    result = np.asarray(result)\n",
    "    \n",
    "    result = 4/sampleNum *  np.sum(result)\n",
    "    return result\n",
    "def Iteration3(sampleNum):\n",
    "    arr = []\n",
    "    for i in range(100):\n",
    "        arr.append(IntegrateWithMC2(sampleNum))\n",
    "    se = pd.Series(arr)\n",
    "    avg = se.mean()\n",
    "    diff = se.std()\n",
    "    print(\"mean:\", avg, \"diff:\", diff)\n",
    "    return (avg, diff)\n",
    "\n",
    "#exec3\n",
    "def exec3():\n",
    "    avg = []\n",
    "    std = []\n",
    "    \n",
    "    times = [10,20,30,40,50,60,70,80,100,200,500]\n",
    "    \n",
    "    for time in times:\n",
    "        tempavg, tempstd = Iteration3(time)\n",
    "        avg.append(tempavg)\n",
    "        std.append(tempstd)\n",
    "    \n",
    " \n",
    "    \n",
    "    se_avg = pd.Series(avg, index=[\"{} times \".format(x) for x in times], name = \"avg\")\n",
    "    se_std = pd.Series(std, index=[\"{} times \".format(x) for x in times], name = \"std\")\n",
    "    \n",
    "    df = pd.concat([se_avg, se_std], axis=1)\n",
    "    print(df)\n",
    "    return df\n",
    "exec3()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\python\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "c:\\python\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n"
     ]
    }
   ],
   "source": [
    "from scipy import integrate\n",
    "import numpy as np\n",
    "def f(x, y):\n",
    "    return y**2 * np.exp(-y**2) / (x**2 * np.exp(-x**2)) + x**3\n",
    "def h(x):\n",
    "    return x\n",
    "v, err = integrate.dblquad(f, -1, 1, lambda x: 2, lambda x: 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29400.31089052881"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "v\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
